package com.fsbm.ctech.maxis.ebpsbackend;

import java.io.File;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Vector;
import java.util.Properties;


import java.io.*;
import java.text.*;
import java.util.*;
import java.util.Date;
import java.sql.*;
import com.fsbm.ctech.maxis.ebpsbackend.ConfigObject;


/**
 *
 * @author Meau
 */

public class SupIndicatorMigration {
    static ConfigObject cfgObj = new ConfigObject();

    public static void main(String[] args) {

		try{

			java.util.Date startTime = new java.util.Date();
			cfgObj.write("\nStart time:"+startTime,"SupIndicatorMigration");

			migrateSubscription();

			java.util.Date endTime = new java.util.Date();
			cfgObj.write("\nEnd time:"+endTime,"SupIndicatorMigration");

		}catch(Exception e){
			cfgObj.write("Exception is..."+e,"SupIndicatorMigration");
		}
    }

    public static void migrateSubscription() throws Exception {
		Connection connection = null;
        PreparedStatement pstmt = null;
        PreparedStatement pstmt1 = null;
        ResultSet rs= null;
        StringBuffer query = new StringBuffer();
        StringBuffer query1 = new StringBuffer();
        ArrayList aResults = new ArrayList();

        try{
			Class.forName(cfgObj.databaseDriver);
			connection = DriverManager.getConnection(cfgObj.databaseSourceName, cfgObj.databaseUserID, cfgObj.databasePassword);

			if (connection!=null){
				query.append("SELECT A.EXTERNALACCOUNTKEY,ckfrconvert.raw_to_char(P.VALUE) FROM PROPERTY P,ACCOUNT A");
				query.append(" WHERE A.ID=P.ID AND P.IDX=28 AND P.VALUE!=ckfrconvert.char_to_raw('N') ");

				pstmt = connection.prepareStatement(query.toString());

                rs   = pstmt.executeQuery();
                int intCtr=0;
                int intUpdateCtr=0;

                while (rs.next()){
					++intCtr;
					String strAcctKeys = rs.getString(1);
					String strSupIndicator = rs.getString(2);
					if(strSupIndicator.equals("Y")){
						strSupIndicator = "1";
					}
					if(strSupIndicator.equals("N")){
						strSupIndicator = "0";
					}
					cfgObj.write("Checking supindicator for account key "+strAcctKeys+" with the supindicator = "+strSupIndicator+" ctr is "+intCtr,"SupIndicatorMigration");
					String strSQL = "UPDATE ACCOUNTDETAILS SET SUPINDICATOR = '"+strSupIndicator+"'  WHERE ACCTKEY = '"+strAcctKeys+"' ";
					pstmt1 = connection.prepareStatement(strSQL);
					if(pstmt1.executeUpdate()>=1){
						++intUpdateCtr;
						cfgObj.write("Updating supindicator for account key "+strAcctKeys+" with the supindicator = "+strSupIndicator+" ctr is "+intUpdateCtr,"SupIndicatorMigration");						;
					}else{
						cfgObj.write("Supindicator for account key "+strAcctKeys+" with the supindicator = "+strSupIndicator+" is not updated","SupIndicatorMigration");						;
					}
					pstmt1.close();
				  }// end while


				pstmt.close();
				rs.close();
			}

		}catch(Exception e){
			throw new Exception("" + e);
		}finally{
			if(connection!=null){
				connection.close();
			}
			if(rs!=null){
				rs=null;
			}
		}// end try

		cfgObj.write("Migrate supindicator has been successfully executed","SupIndicatorMigration");
    }

}